package cn.stronglink.assetmanage.business.setting.syncset;

import android.annotation.TargetApi;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.annotation.RequiresApi;
import android.support.v4.app.Fragment;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;
import cn.stronglink.assetmanage.R;
import cn.stronglink.assetmanage.dao.DaoManager;
import cn.stronglink.assetmanage.entity.SyncContent;

/**
 * @author zww
 * 2018 2018/6/18 8:46
 */
public class SystemSyncContentFragment extends Fragment implements SyncListener {
    @BindView(R.id.recycleSynContent)
    RecyclerView recycleSynContent;
    private SystemSyncContentAdapter adapter;
    private Unbinder unbinder;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_setting_syscontent, container, false);
        unbinder = ButterKnife.bind(this, view);
        return view;
    }

    @Override
    public void onPause() {
        super.onPause();
        adapter.notifyDataSetChanged();
    }

    @RequiresApi(api = Build.VERSION_CODES.KITKAT)
    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        adapter = new SystemSyncContentAdapter(getContext());
        adapter.setOnItemClickListener(this);
        HashMap<String, Integer> stringIntegerHashMap = new HashMap<>();
        stringIntegerHashMap.put(RecyclerViewSpacesItemDecoration.TOP_DECORATION, 25);//top间距

        stringIntegerHashMap.put(RecyclerViewSpacesItemDecoration.BOTTOM_DECORATION, 25);//底部间距

        stringIntegerHashMap.put(RecyclerViewSpacesItemDecoration.LEFT_DECORATION, 11);//左间距

        stringIntegerHashMap.put(RecyclerViewSpacesItemDecoration.RIGHT_DECORATION, 11);//右间距

        recycleSynContent.addItemDecoration(new RecyclerViewSpacesItemDecoration(stringIntegerHashMap));
        //recycleSynContent.setLayoutManager(new LinearLayoutManager(getContext(), LinearLayoutManager.HORIZONTAL, false));
        recycleSynContent.setLayoutManager(new GridLayoutManager(getContext(), 2, GridLayoutManager.HORIZONTAL, false));
        recycleSynContent.setAdapter(adapter);
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    @TargetApi(Build.VERSION_CODES.KITKAT)
    @Override
    public void onSynItemClick(View view, int postion) {
        if (!adapter.list.get(postion).getIsSelected()) {
            SyncContent entity = adapter.list.get(postion);
            entity.setIsSelected(true);
            DaoManager.getDaoSession(getContext()).getSyncContentDao().update(entity);
        } else {
            SyncContent entity = adapter.list.get(postion);
            entity.setIsSelected(false);
            DaoManager.getDaoSession(getContext()).getSyncContentDao().update(entity);
        }
        adapter.notifyDataSetChanged();
    }
}
